//
//  detailView.m
//  项目三
//
//  Created by rimi on 15/10/13.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "detailView.h"
@interface detailView () {

    UIImageView *_imageView;
}

@end

@implementation detailView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeUserInterface {

    self.backgroundColor = [UIColor clearColor];
    self.frame = DHFlexibleFrame(CGRectMake(0, 1024, 708, 922));
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(self.bounds)];
    imageView.image = [UIImage imageNamed:@"设置主背景"];
    imageView.alpha = 0.9;
    [self addSubview:imageView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.scrollView];
    
    [self addSubview:self.title];
    
    UIView *upLight = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(70, 113, 557, 2))];
    upLight.backgroundColor = COLOR(197, 171, 98, 1);
    [self addSubview:upLight];
    
    UIView *downLight = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(70, 812, 557, 2))];
    downLight.backgroundColor = COLOR(197, 171, 98, 1);
    [self addSubview:downLight];
    
    UIViewController *topVC = [self appRootViewController];
    [topVC.view addSubview:self];
    
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    [self.scrollView addSubview:self.collectBtn];
}

- (void)respondsToCancelBtn:(UIButton *)sender {
    
    [self removeFromSuperview];
}

- (UIViewController *)appRootViewController {
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}
#pragma mark - system methods
- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview == nil) {
        return;
    }
    CGRect afterFrame = DHFlexibleFrame(CGRectMake(0, 0, 708, 922));
    
    [UIView animateWithDuration:0.25f delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        
    } completion:^(BOOL finished) {
        
    }];
    [super willMoveToSuperview:newSuperview];
}
- (void)removeFromSuperview {
    CGRect afterFrame = CGRectMake(0, 944, 708, 922);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}
- (void)respondsToCollectBtn:(UIButton *)sender {

    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(sender.frame), CGRectGetMinY(sender.frame), 135, 85))];
    }
    _imageView.image = [UIImage imageNamed:@"收藏成功"];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    if (![self.subviews containsObject:_imageView]) {
        [self addSubview:_imageView];
    }
    
    // 动画选项设定
    animation.duration = 0.5; // 动画持续时间
    animation.repeatCount = 1; // 重复次数
    
    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:0.3]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.0]; // 结束时的倍率
    
    // 添加动画
    [_imageView.layer addAnimation:animation forKey:@"scale-layer"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_imageView removeFromSuperview];
    });
}
#pragma mark - getter
- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(70, 113, 600, 700))];
        _scrollView.contentSize = CGSizeMake(557, 1700);
    }
    return _scrollView;
}
- (UILabel *)title {

    if (!_title) {
        _title = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(97, 0, 500, 115))];
        _title.textAlignment = NSTextAlignmentCenter;
        _title.font = [UIFont boldSystemFontOfSize:20];
    }
    return _title;
}
- (UIButton *)cancelBtn {

    if (!_cancelBtn) {
        _cancelBtn = ({
            UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [cancelBtn setFrame:DHFlexibleFrame(CGRectMake(639, 15, 44, 37))];
            [cancelBtn setBackgroundImage:[UIImage imageNamed:@"关闭_05"] forState:UIControlStateNormal
             ];
            [cancelBtn addTarget:self action:@selector(respondsToCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
            cancelBtn;
        });
    }
    return _cancelBtn;
}
- (UIButton *)collectBtn {

    if (!_collectBtn) {
        _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectBtn setFrame:DHFlexibleFrame(CGRectMake(470, 5, 85, 35))];
        [_collectBtn setBackgroundImage:[UIImage imageNamed:@"收藏(1)"] forState:UIControlStateNormal];
        [_collectBtn setBackgroundImage:[UIImage imageNamed:@"已收藏(1)"] forState:UIControlStateSelected];
        [_collectBtn addTarget:self action:@selector(respondsToCollectBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectBtn;
}
@end
